//We'll enclose our code in a self-invoking function to avoid
//inadvertent pollution of the global namespace

(function(){
	// this sets the background color of the master UIView (when there are no windows/tab groups on it)
	Titanium.UI.setBackgroundColor('#000');

	var MasterView = require("/views/MasterView");
	var ReaderHomeView = require("/views/ReaderHomeView");
	var ReaderDetailView = require("/views/ReaderDetailView");
	var SearchHomeView = require("/views/SearchHomeView");
	var QuizHomeView = require("/views/QuizHomeView");
	var QuizDetailView = require ("/views/QuizDetailView");
	var GlossaryView = require("/views/GlossaryView");
	
	var tabGroup = Titanium.UI.createTabGroup();

	var mainWindow = Titanium.UI.createWindow({
		title: "PMP Prep",
		backgroundColor: "#FFF",
		fullscreen: false,	//[Android: makes the window a "heavyweight" window (thereby allowing the back button to work with it)]
		exitOnClose: true, //[Android: make the application exit if the back button is pressed from the main window]
		navBarHidden: false,
		tabBarHidden: true
	});
	var tab = Titanium.UI.createTab({
		icon:'KS_nav_views.png', //Irrelevant; we are hiding the tab bar
		title:'Tab 1', //Irrelevant; we are hiding the tab bar
		window:mainWindow
	});
	
	var masterView = new MasterView();
	
	mainWindow.add(masterView);

	masterView.addEventListener("click", function(e){
	    
	    //Reading menu option and controller code
		if (e.rowData.shortTitle == "Read"){	
			
			var readerHomeView = new ReaderHomeView({
				title: e.rowData.shortTitle
			});
	
			tab.open(readerHomeView, {animated: true});
		
			readerHomeView.addEventListener("click", function(e){
				var readerDetailView = new ReaderDetailView ({
					title: e.rowData.detailTitle,
					detail: e.rowData.detail
				});
				tab.open(readerDetailView, {animated: true});
			});		
		}

		//Search menu option and controller code
		//TODO: perform actual search and provide real results
		if (e.rowData.shortTitle == "Search"){	
			var searchHomeView = new SearchHomeView({
				title: e.rowData.shortTitle
			});
	
			tab.open(searchHomeView, {animated: true});
		}
		
		//Quiz menu option and controller code
		//TODO: Determine if the correct answer was selected
		//TODO: Calculate latest quiz score to be displayed on results page once no more questions exist
		//TODO: Current code is hardcoded to only handle up to 3 quiz questions per chapter
		if (e.rowData.shortTitle == "Quiz"){	
			var quizHomeView = new QuizHomeView({
				title: e.rowData.shortTitle
			});
	
			tab.open(quizHomeView, {animated: true});
		
			quizHomeView.addEventListener("click", function(e){
				var title = e.rowData.detailTitle;
				var quizDetailView = new QuizDetailView ({
					title: title,
					currQuestNum: 1,
				});
				tab.open(quizDetailView, {animated: true});
				
				quizDetailView.addEventListener("click", function(e){
					console.log('Option Selected: '+ e.index);
					
					quizDetailView2 = new QuizDetailView ({
						title: title,
						currQuestNum: 2,
					});
					tab.open(quizDetailView2, {animated: true});
					
					quizDetailView2.addEventListener("click", function(e){
						console.log('Option Selected: '+ e.index);
						
						quizDetailView3 = new QuizDetailView ({
							title: title,
							currQuestNum: 3,
						});
						tab.open(quizDetailView3, {animated: true});
					});
				});
			});
		}

		//Glossary menu option and controller code
		if (e.rowData.shortTitle == "Glossary"){	

			var glossaryView = new GlossaryView({
				title: e.rowData.shortTitle
			});
	
			tab.open(glossaryView, {animated: true});
		}

	});

	tabGroup.addTab(tab);
	
	// open tab group
	tabGroup.open();

})();